package com.hoshiicloud.as2c.minipro.controller;

import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.as2c.minipro.dto.OrderDTO;
import com.hoshiicloud.as2c.minipro.feign.OrderFeignService;
import com.hoshiicloud.as2c.minipro.param.StoreVerificationUpdateParam;
import com.hoshiicloud.common.utils.AESUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ygd
 * @title: VerificationController
 * @projectName hoshiicloud-root
 * @description: 核销Controller
 * @date 2019/6/26
 */
@RestController
@Slf4j
@RequestMapping("/verification")
@Api(value = "核销接口", tags = {"核销操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class VerificationController {

    // 订单微服务Service
    @Autowired
    private OrderFeignService orderFeignService;

    /**
     * 店铺核销
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/store/order")
    @ApiOperation(value = "店铺核销", notes = "店铺核销", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response storeVerification(@RequestBody StoreVerificationUpdateParam param) {

        if (param == null) {
            return Response.failed("非法请求");
        }

        try {
            //对称解密核销码
            String orderId = AESUtil.decrypt(param.getCode());

            //根据主订单id查询订单
            Response<OrderDTO> orderDTOResponse = orderFeignService.viewOrder(Long.parseLong(orderId));

            if (orderDTOResponse == null || null == orderDTOResponse.getData()) {
                return Response.failed("非法请求");
            }

            OrderDTO orderDTO = orderDTOResponse.getData();

            if (orderDTO.getStoreid() != param.getStoreId()) {
                return Response.failed("核销失败");
            }
            return orderFeignService.verificationOrder(orderDTO.getOrderId());


        } catch (Exception e) {

            return Response.failed("非法请求");
        }

    }


}
